import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';

class PdfLoader {
  static Future<List<int>> loadPdfFromUrl(String url) async {
    final httpClient = HttpClient();
    // httpClient.post(host, port, path)
    final request = await httpClient.getUrl(Uri.parse(url));
    final response = await request.close();

    if (response.statusCode == 200) {
      return await consolidateHttpClientResponseBytes(response);
    } else {
      throw Exception('HTTP请求失败，状态码: ${response.statusCode}');
    }
  }
}

Future<void> sendRequest() async {
  // 创建HttpClient实例
  final client = HttpClient();

  try {
    // 打开HTTP连接
    final request = await client.postUrl(Uri.parse('https://example.com/api/endpoint'));

    // 设置请求头
    request.headers.set('content-type', 'application/json');

    // 准备请求体数据
    final Map<String, dynamic> data = {
      'name': 'John Doe',
      'age': 30,
      'email': 'johndoe@example.com'
    };

    // 将数据转换为JSON字符串并写入请求体
    final jsonData = json.encode(data);
    request.write(jsonData);

    // 发送请求并获取响应
    final response = await request.close();

    // 处理响应
    final responseBody = await response.transform(utf8.decoder).join();
    print('Response status: ${response.statusCode}');
    print('Response body: $responseBody');

  } catch (e) {
    print('Error: $e');
  } finally {
    // 关闭HttpClient
    client.close();
  }
}